package com.ruoyi.web.controller.warning;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.warning.domain.entity.Device;
import com.ruoyi.warning.domain.entity.DevicesPing;
import com.ruoyi.warning.domain.query.DeviceQuery;
import com.ruoyi.warning.domain.vo.DeviceVO;
import com.ruoyi.warning.mapper.DevicesMapper;
import com.ruoyi.warning.service.IDevicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/devices")
@Component("devices")
@Api(tags = "设备", description = "用于处理设备相关操作的控制器")
public class DevicesController {
    @Resource
    private IDevicesService devicesService;
    @Resource
    private DevicesMapper devicesMapper;
    @ApiOperation("获取所有设备")
    @GetMapping(value = "/getAllDevices")
    public R getAllDevices(DeviceQuery deviceQuery)
    {
        return R.ok(devicesService.getAllDevices(deviceQuery));
    }
    @ApiOperation("分页获取所有设备")
    @GetMapping(value = "/getAllDevicesPage")
    public R getAllDevicesPage(DeviceQuery deviceQuery)
    {
        return R.ok(devicesService.getAllDevicesPage(deviceQuery));
    }

    @ApiOperation("获取设备的详细信息")
    @GetMapping(value = "/getDeviceDetail/{deviceId}")
    public R getDeviceDetail(@PathVariable String deviceId) throws IOException {
        return R.ok(devicesService.getDeviceDetail(deviceId));
    }



    @ApiOperation("修改设备的详细信息")
    @PostMapping(value = "/updateDetail")
    public R updateDetail(@RequestBody Device device)
    {
        return R.ok(devicesService.updateDetail(device));
    }

    @ApiOperation("批量修改设备的详细信息")
    @PostMapping(value = "/updateDetailList")
    public R updateDetailList(@RequestBody List<Device> list)
    {
        return R.ok(devicesService.updateBatchById(list));
    }

    @ApiOperation("重置设备的XY坐标")
    @PostMapping(value = "/resetXY")
    public R resetXY(@RequestBody List<String> list)
    {
        return R.ok(devicesService.resetXY(list));
    }

    @ApiOperation("获取设备的告警数量")
    @GetMapping(value = "/getDeviceWarningCount/{deviceId}")
    public R getDeviceWarningCount(@PathVariable String deviceId)
    {
        return R.ok(devicesService.getDeviceWarningCount(deviceId));
    }

    @ApiOperation("设备监控状态改变（前端执行，后端记录日志）")
    @Log(title = "设备监控状态改变", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateDeviceMonitor")
    public R updateDeviceMonitor(@RequestBody List<Device> list){
        return R.ok(null);
    }

    @ApiOperation("删除设备（删除拓扑和端口）")
    @Log(title = "设备删除", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteDevice/{id}")
    public R deleteDevice(@PathVariable String id){
        devicesService.deleteDevice(id);
        return R.ok(null);
    }

    @ApiOperation("分页展示无线设备")
    @GetMapping(value = "/wirelessPage")
    public R wirelessPage(DeviceQuery deviceQuery){
        return R.ok(devicesService.wirelessPage(deviceQuery));
    }

    @ApiOperation("获取设备网络信息")
    @GetMapping(value = "/getDeviceNet/{deviceId}")
    public R getDeviceNet(@PathVariable String deviceId) throws IOException {
        return R.ok(devicesService.getDeviceNet(deviceId));
    }

    @ApiOperation("获取设备PING信息")
    @GetMapping(value = "/getDevicePING/{deviceId}")
    public R getDevicePING(@PathVariable String deviceId) throws IOException {
        return R.ok(devicesService.getDevicePING(deviceId));
    }

    @ApiOperation("获取设备SNMP信息")
    @GetMapping(value = "/getDeviceSNMP/{deviceId}")
    public R getDeviceSNMP(@PathVariable String deviceId) throws IOException {
        return R.ok(devicesService.getDeviceSNMP(deviceId));
    }

    @ApiOperation("新增设备的附加功能")
    @PostMapping(value = "/addDevice/{deviceId}")
    public R addDevice(@PathVariable Integer deviceId) {
        return R.ok(devicesService.addDevice(deviceId));
    }

    @PostMapping(value = "/exportExcel", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public void exportExcel(@ModelAttribute DeviceQuery deviceQuery, HttpServletResponse response) throws IOException {
        Long userId = SecurityUtils.getUserId();
        List<String> roleDevice = devicesMapper.getRoleDevice(userId);
        if(roleDevice.size()>0){
            deviceQuery.setDeviceList(roleDevice);
        }
        List<DeviceVO> list = devicesMapper.getAllDevicesPage(deviceQuery);
        ExcelUtil<DeviceVO> util = new ExcelUtil<>(DeviceVO.class);
        util.exportExcel(response, list, "资产表");
    }

    @ApiOperation("刷新设备的在线状态")
    @GetMapping(value = "/refresh")
    public R refresh() {
        devicesService.refresh();
        return R.ok();
    }

    @ApiOperation("记录设备的ping时间")
    @GetMapping(value = "/savePing")
    public R savePing() {
        devicesService.savePing();
        return R.ok();
    }

    @ApiOperation("获取设备的ping")
    @GetMapping(value = "/getPing")
    public R getPing(DeviceQuery deviceQuery) {
        List<DevicesPing> list = devicesService.getPing(deviceQuery);
        return R.ok(list);
    }

    @Anonymous
    @ApiOperation("获取设备的mempool图表")
    @GetMapping(value = "/getDeviceMempoolChart")
    public R getDeviceMempoolChart(DeviceQuery deviceQuery) {
        return R.ok(devicesService.getDeviceMempoolChart(deviceQuery));
    }
    @Anonymous
    @ApiOperation("获取设备的pro图表")
    @GetMapping(value = "/getDeviceProcessorChart")
    public R getDeviceProcessorChart(DeviceQuery deviceQuery) {
        return R.ok(devicesService.getDeviceProcessorChart(deviceQuery));
    }
}
